package cn.wsgfz.test;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import com.larvalabs.svgandroid.SVG;
import com.larvalabs.svgandroid.SVGParser;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Picture;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import cn.wsgfz.testsvgapp.R;
import cn.wsgfz.widget.PictureDrawablePlus;

public class MainActivity extends BaseActivity {
	private Button mDisplayResultBtn1, mDisplayResultBtn2, mDisplayResultBtn3;
	private	Button mDifficultDataBtn, mDifficultImageBtn;
	private ListView mContentList;
	private HSvgPngAdapter mAdapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.svg_layout);
		
		initAssembly();
		initViewListener();
		
		Picture p = getPicture(R.raw.icon01);
		List<Integer> ids = null;
		ids = Arrays.asList(R.drawable.a128, R.drawable.a80, R.drawable.a44, R.drawable.a22, R.drawable.a16);
		
		mAdapter = new HSvgPngAdapter(this, getDrawList(ids, p));	
		mContentList.setAdapter(mAdapter);
	}

	private void initViewListener() {
		mDisplayResultBtn1.setOnClickListener(getDisplayListener());
		mDisplayResultBtn2.setOnClickListener(getDisplayListener());
		mDisplayResultBtn3.setOnClickListener(getDisplayListener());
		mDifficultDataBtn.setOnClickListener(getDiffcultListener());
		mDifficultImageBtn.setOnClickListener(getDiffImgListener());
	}

	private OnClickListener getDiffImgListener() {
		return new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				List<Drawable> resList = new CopyOnWriteArrayList<Drawable>();
				Resources res = MainActivity.this.getResources();
				
				List<Integer> ids = Arrays.asList(R.drawable.d370, R.drawable.d300, R.drawable.d200);
				Picture p = getPicture(R.raw.logo);
				
				
				for(int i = 0; i < ids.size(); i ++) {
					Drawable draw = res.getDrawable(ids.get(i));
					int w  = draw.getIntrinsicWidth();
					float pw = (float)p.getWidth();
					float wScale = w / pw;
					
					resList.add(draw);
					resList.add(new PictureDrawablePlus(p, wScale));
				}
				
				VSvgPngAdapter adapter = new VSvgPngAdapter(MainActivity.this, resList, 1);
				mContentList.setAdapter(adapter);
			}
		};
	}

	private OnClickListener getDiffcultListener() {
		return new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setClass(MainActivity.this, DifficultDataActivity.class);
				MainActivity.this.startActivity(intent);
			}
		};
	}

	private OnClickListener getDisplayListener() {
		return new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				int currentId = v.getId();
				Picture p = null;
				List<Integer> ids = null;
				
				if(mDisplayResultBtn1.getId() == currentId) {
					p = getPicture(R.raw.icon01);
					ids = Arrays.asList(R.drawable.a128, R.drawable.a80, R.drawable.a44, R.drawable.a22, R.drawable.a16);
					mContentList.setAdapter(new HSvgPngAdapter(MainActivity.this, getDrawList(ids, p)));
				} else if(mDisplayResultBtn2.getId() == currentId) {
					p = getPicture(R.raw.icon02);
					ids = Arrays.asList(R.drawable.b128, R.drawable.b80, R.drawable.b44, R.drawable.b22, R.drawable.b16);
					mContentList.setAdapter(new HSvgPngAdapter(MainActivity.this, getDrawList(ids, p)));
				} else if(mDisplayResultBtn3.getId() == currentId) {
					p = getPicture(R.raw.icon03);
					ids = Arrays.asList(R.drawable.c128, R.drawable.c80, R.drawable.c44, R.drawable.c22, R.drawable.c16);
					mContentList.setAdapter(new HSvgPngAdapter(MainActivity.this, getDrawList(ids, p)));
				}
			}
		};
	}

	private void initAssembly() {
		mDisplayResultBtn1 = (Button) findViewById(R.id.display_btn_1);
		mDisplayResultBtn2 = (Button) findViewById(R.id.display_btn_2);
		mDisplayResultBtn3 = (Button) findViewById(R.id.display_btn_3);
		mDifficultDataBtn  = (Button) findViewById(R.id.display_diffcult_data);
		mDifficultImageBtn = (Button) findViewById(R.id.display_dif_imager);
		
		mContentList = (ListView) findViewById(R.id.content_list);
	}

	private Picture getPicture(int rawId) {
		SVG svg = SVGParser.getSVGFromResource(getResources(), rawId);
		Picture p = svg.getPicture();
		return p;
	}

	private List<Drawable> getDrawList(List<Integer> ids, Picture p) {
		Resources res = getResources();
		List<Drawable> rList = new CopyOnWriteArrayList<Drawable>();
		
		for(int i = 0; i < ids.size(); i ++) {
			Drawable draw = res.getDrawable(ids.get(i));
			rList.add(draw);
			int w = draw.getIntrinsicWidth();
			float wScale = (float)w / (float)p.getWidth();
			rList.add(new PictureDrawablePlus(p, wScale));
		}
		
		return rList;
	}
}
